﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Text;
using WAD.WEB.AppCode;
using WAD.Model;
using WAD.DAL;
using WAD.BLL;

namespace WAD.WEB.Client.Ajax
{
    /// <summary>
    /// 发财：分享业务接口
    /// </summary>
    public class FC_Shared : AjaxBase
    {
        public override void AjaxMethodDispatch(String identity)
        {
            switch (identity)
            {
                case "insert": this.Insert(); break;
                case "shared": this.Shared(); break;
                case "gethongbao": this.GetHongBao(); break;
                case "getlistshared": this.GetListShared(); break;
                case "getlistshared_no": this.GetListShared_No(); break;
                case "shared_no": this.Shared_No(); break;
                case "getdetaillist": this.GetDetailList(); break;
                case "get": this.Get(); break;
            }
        }
        /// <summary>
        /// 分享者：客户端分享，添加一条记录
        /// </summary>
        [QD.Web.Ajax.AjaxMethod]
        public void Insert()
        {
            MFCShared m = new MFCShared();
            m.article_id = Parse.Int("article_id", 0);
            m.user_id = Parse.Int("from_user_id", 0);   //文章的发布者ID
            m.by_user_id = Parse.Int("user_id", 0);     //分享者ID
            m.platform = Parse.Int("platform", 1);
            m.money_id = Parse.Int("money_id", 0);
            m.date = DateTime.Now;
            m.areaname = "";
            m.price = 0;
            m.state = 0;
            //检查是否分享过
            int id = DAOFCShared.GetLastId(m.user_id, m.article_id, m.by_user_id);
            if (id < 1)
            {
                id = DAOFCShared.Insert(m);         //添加分享记录
            }
            else
            {
                DAOFCShared.UpdateShared(id, m.money_id, DateTime.Now);
            }

            if (m.money_id > 0)
                DAOFCSharedMoney.Lock(m.money_id);  //锁定红包

            if (id > 0)
                Success(id);
            else
                Fail();
        }

        /// <summary>
        /// 分享者： 上传朋友圈截图，并更新红包价格
        /// </summary>
        [QD.Web.Ajax.AjaxMethod]
        public void Shared()
        {
            IsLogin();
            int user_id = Parse.Int("user_id", 0);
            int id = Parse.Int("id", 0);            //分享ID
            int money_id = Parse.Int("money_id", 0);  //红包明细表 主键ID
            string picurl = Parse.String("picurl", "");
            if (id < 1)
                ParamError();
            if (money_id < 1)
                JsonResult(11, "红包参数错误");

            string msg = FCSharedBLL.Shared(id, user_id, money_id, picurl, WJXConfig.EncryptConfig.DES_KEY);
            if (string.IsNullOrEmpty(msg))
                Success();
            JsonResult(1, msg, null);
        }

        /// <summary>
        /// 分享者：获取红包前几名
        /// </summary>
        [QD.Web.Ajax.AjaxMethod]
        public void GetHongBao()
        {
            IsLogin();
            int user_id = Parse.Int("user_id", 0);
            int pageIndex = Parse.Int("page_index", 1);//当前页
            int article_id = Parse.Int("article_id", 0);
            if (article_id < 1)
                DataEmpty();
            PageData pg = DAOFCShared.GetList(5, "article_id=" + article_id, "v_fc_shared_money", "id,nick_name,user_logo,price,date", " price desc");
            if (pg.itemCount == 0)
                DataEmpty();
            Success(pg);
        }

        /// <summary>
        ///分享者： 获取分享列表
        /// </summary>
        [QD.Web.Ajax.AjaxMethod]
        public void GetListShared()
        {
            IsLogin();
            int user_id = Parse.Int("user_id", 0);
            int pageIndex = Parse.Int("page_index", 1);//当前页
            //显示 红包金额>0 , 并且未审核、已审核的列表(30分钟内未上传截图，标记成失效状态)
            //05-19注释
            //PageData pg = DAOFCShared.GetListPage("id,title,logo,price,article_id,money_id,state,date", "id desc", pageIndex, 20, string.Format("(price>0 and isnull(is_pass,0)<2 and by_user_id={0}) or (money_id>0 and isnull(is_pass,0)<2 and by_user_id={0} and datediff(mi,date,getdate())<60)", user_id), "v_fc_shared_article");
            PageData pg = DAOFCShared.GetListPage("id,title,logo,price,article_id,money_id,state,date", "id desc", pageIndex, 20, string.Format("by_user_id={0} and price>0 and isnull(is_pass,0)<2", user_id), "v_fc_shared_article");
            if (pg.itemCount == 0)
                DataEmpty();
            Success(pg);
        }

        /// <summary>
        /// 分享者: 获取已拒绝的广告
        /// </summary>
        [QD.Web.Ajax.AjaxMethod]
        public void GetListShared_No()
        {
            IsLogin();
            int user_id = Parse.Int("user_id", 0);
            int pageIndex = Parse.Int("page_index", 1);//当前页
            //已拒绝、审核中的列表
            List<Dictionary<string, object>> list = DAOFCShared.GetList(string.Format("price>0 and is_pass>1 and by_user_id={0}", user_id), 10, "v_fc_shared_article", "id,title,logo,money_id,is_pass,date");
            if (list.Count == 0)
                DataEmpty();
            Success(list);
        }

        /// <summary>
        /// 分享者： 重新上传朋友圈截图(不更新价格)
        /// </summary>
        [QD.Web.Ajax.AjaxMethod]
        public void Shared_No()
        {
            IsLogin();
            int id = Parse.Int("id", 0);            //分享ID
            string picurl = Parse.String("picurl", "");
            if (id < 1)
                ParamError();
            if (DAOFCShared.changeIsPass(id, 3, picurl))
                Success();
            Fail();
        }


        /// <summary>
        ///广告主： 获取广告效果列表
        /// </summary>
        [QD.Web.Ajax.AjaxMethod]
        public void GetDetailList()
        {
            IsLogin();
            int article_id = Parse.Int("article_id", 0);
            int pageIndex = Parse.Int("page_index", 1); //当前页
            PageData pg = DAOFCShared.GetListPage("id,mobile,wx_name,picurl,date,area1,area2,area3", "id desc", pageIndex, 20, "picurl!='' and article_id=" + article_id, "v_fc_shared_setting");
            if (pg.itemCount == 0)
                DataEmpty();
            Success(pg);
        }

        /// <summary>
        ///广告主： 获取一条分享详情
        /// </summary>
        [QD.Web.Ajax.AjaxMethod]
        public void Get()
        {
            IsLogin();
            int id = Parse.Int("id", 0);
            if (id < 1) ParamError();

            Dictionary<string, object> model = DAOFCRecord.Get("v_fc_shared_setting", "id=" + id, "id,article_id,price,picurl,date,wx_name,area1,area2,area3");
            if (model == null) DataEmpty();

            model.Add("title", "");
            Dictionary<string, object> article = DAOFCArticle.Get("fc_article", "id=" + model["article_id"], "title,op_price");
            if (article != null)
            {
                model["title"] = article["title"];
                //model["price"] = article["op_price"];   //均价
            }

            Success(model);
        }
    }
}